package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.WxGetJsSDKSignRequeset;
import cc.rengu.igas.bsps.facade.response.WxGetJsSDKSignResponse;
import cc.rengu.igas.channel.wechat.facade.base.Header;
import cc.rengu.igas.channel.wechat.facade.request.GetJsapiTicketRequest;
import cc.rengu.igas.channel.wechat.facade.response.GetJsapiTicketResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangxuran
 * @version 1.0.0
 * @date 2020.04.07
 */
public class WxGetJsSDKSignService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        WxGetJsSDKSignRequeset wxGetJsSDKSignRequeset = new WxGetJsSDKSignRequeset();
        ConvertUtil.convertOutput(wxGetJsSDKSignRequeset);
        return wxGetJsSDKSignRequeset;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        WxGetJsSDKSignRequeset wxGetJsSDKSignRequeset = (WxGetJsSDKSignRequeset) request;
        String url = wxGetJsSDKSignRequeset.getUrl();
        String instId = wxGetJsSDKSignRequeset.getHeader().getInstId();
        if (StringUtil.isEmptyOrNull(url) || StringUtil.isEmptyOrNull(instId)) {
            rglog.error("微信签名交易必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /*dubbo请求初始化*/
        GetJsapiTicketRequest getJsapiTicketRequest = new GetJsapiTicketRequest();
        //获取通道标识,设置通道标识（需要修改，通道标识和通道序号，这个是从交易码映射表中获取）
        List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList();
        List<ChannelCallCfg> msgDstchannelCallCfgList = channelCallCfgList.stream().filter(item -> item.getCallChannelId().equals("WXZF")).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(msgDstchannelCallCfgList)) {
            rglog.error("微信通道交易未配置！");
            throw new BizException(RespCodeEnum.CHANNEL_TXN_NOT_SUPPORTED.getRespCode(), RespCodeEnum.CHANNEL_TXN_NOT_SUPPORTED.getRespDesc());
        }
        ChannelCallCfg channelCallCfg = msgDstchannelCallCfgList.get(0);
        Header header = new Header();
        header.setInstId(instId);
        header.setBizType(channelCallCfg.getBizType());
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setChanlId(channelCallCfg.getMsgSrcId());
        header.setTxnNum(channelCallCfg.getCallChannelTxn());
        header.setTraceNo(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
        header.setTransDate(DateUtil.getCurrentDate());
        header.setTransTime(DateUtil.getCurrentTime());
        header.setVersion("1.0.0");
        getJsapiTicketRequest.setHeader(header);
        getJsapiTicketRequest.setPageUrl(url);
        DubboService dubboService = new DubboServiceImpl();
        Result<GetJsapiTicketResponse> result = (Result<GetJsapiTicketResponse>) dubboService.callDubboService("wechatPubService", "getJsapiTicket", getJsapiTicketRequest);
        if (null == result || !result.isSuccess() || null == result.getResult() || !RespCodeEnum.TRANS_SUCCESS.getRespCode().equals(result.getResult().getRespCode())) {
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        WxGetJsSDKSignResponse wxGetJsSDKSignResponse = new WxGetJsSDKSignResponse();
        wxGetJsSDKSignResponse.setJsapiTicket(result.getResult().getJsapiTicket());
        wxGetJsSDKSignResponse.setNonceStr(result.getResult().getNonceStr());
        wxGetJsSDKSignResponse.setTimestamp(result.getResult().getTimestamp());
        wxGetJsSDKSignResponse.setWechatSign(result.getResult().getWechatSign());
        BizResponse<WxGetJsSDKSignResponse> bizResponse = new BizResponse<>();
        bizResponse.setResult(wxGetJsSDKSignResponse);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        WxGetJsSDKSignResponse wxGetJsSDKSignResponse = (WxGetJsSDKSignResponse) bizResponse.getResult();
        ConvertUtil.convertInput(wxGetJsSDKSignResponse);
    }
}
